package com.ruoyi.system.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.domain.SysBusiness;
import com.ruoyi.system.domain.SysOrder;
import com.ruoyi.system.domain.SysParking;
import com.ruoyi.system.domain.SysParkingSpaces;
import com.ruoyi.system.domain.parm.IdParm;
import com.ruoyi.system.domain.parm.SysParkingSpacesParm;
import com.ruoyi.system.service.ISysBusinessService;
import com.ruoyi.system.service.ISysOrderService;
import com.ruoyi.system.service.ISysParkingService;
import com.ruoyi.system.service.ISysParkingSpacesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.UUID;

/**
 * 【请填写功能名称】Controller
 * 
 * @author ruoyi
 * @date 2024-03-31
 */
@Api(tags = "商家停车位")
@RestController
@RequestMapping("/bus/spaces")
public class BusParkingSpacesController extends BaseController
{
    @Autowired
    private ISysParkingSpacesService sysParkingSpacesService;
    @Autowired
    private ISysBusinessService sysBusinessService;
    @Autowired
    private ISysParkingService sysParkingService;
    @Autowired
    private ISysOrderService sysOrderService;
    /**
     * 查询【请填写功能名称】列表
     */
    @ApiOperation(value = "商家查询停车位list")
    @GetMapping("/list")
    public TableDataInfo list(SysParkingSpaces sysParkingSpaces) throws Exception {
        SysBusiness bu = sysBusinessService.getUserInfo();
        if(bu==null){
            throw new Exception("用户未登陆");
        }
        sysParkingSpaces.setBusinessId( bu.getId());



        startPage();
        List<SysParkingSpaces> list = sysParkingSpacesService.selectSysParkingSpacesList(sysParkingSpaces);
        return getDataTable(list);
    }


    /**
     * 获取【请填写功能名称】详细信息
     */
    @ApiOperation(value = "商家查询停车位")
    @GetMapping(value = "/info")
    public AjaxResult getInfo(@RequestParam(name = "id",required = true) String id)
    {
        return success(sysParkingSpacesService.selectSysParkingSpacesById(id));
    }

    /**
     * 新增【请填写功能名称】
     */
    @ApiOperation(value = "商家添加停车位")
    @PostMapping("add")
    public AjaxResult add(@RequestBody @Valid SysParkingSpacesParm sysParkingSpacesParm) throws Exception {
        SysBusiness bu = sysBusinessService.getUserInfo();
        if(bu==null){
            throw new Exception("用户未登陆");
        }

        // 查询停车场是否有效
        SysParking parking = sysParkingService.selectSysParkingById(sysParkingSpacesParm.getParkingId());

        if(parking==null){
            throw new Exception("停车场id无效");
        }
        if(!parking.getBusinessId().equals(bu.getId())){
            throw new Exception("停车场id无效");
        }

        // 查询停车位名称是否重复
        List<SysParkingSpaces> ret = sysParkingSpacesService.selectSysParkingSpacesByName(sysParkingSpacesParm.getName(), sysParkingSpacesParm.getParkingId());
        if(ret.size()>0){
            throw new Exception("停车位名称已经存在");

        }

        SysParkingSpaces sysParkingSpaces = new SysParkingSpaces();
        BeanUtils.copyProperties(sysParkingSpacesParm, sysParkingSpaces);

        sysParkingSpaces.setBusinessId( bu.getId());
        sysParkingSpaces.setId(UUID.randomUUID().toString());
        return toAjax(sysParkingSpacesService.insertSysParkingSpaces(sysParkingSpaces));
    }

    /**
     * 修改【请填写功能名称】
     */
    @ApiOperation(value = "商家修改停车位")
    @PutMapping("edit")
    public AjaxResult edit(@RequestBody @Valid SysParkingSpacesParm sysParkingSpacesParm) throws Exception {
        SysBusiness bu = sysBusinessService.getUserInfo();
        if(bu==null){
            throw new Exception("用户未登陆");
        }
        if(sysParkingSpacesParm.getId()==null){
            throw new Exception("id 不能为空");
        }

        // 查询停车场是否有效
        SysParking parking = sysParkingService.selectSysParkingById(sysParkingSpacesParm.getParkingId());

        if(parking==null){
            throw new Exception("停车场id无效");
        }
        if(!parking.getBusinessId().equals(bu.getId())){
            throw new Exception("停车场id无效");
        }
        // 查询停车位名称是否重复
        List<SysParkingSpaces> ret = sysParkingSpacesService.selectSysParkingSpacesByName(sysParkingSpacesParm.getName(), sysParkingSpacesParm.getParkingId());
        if(ret.size()>0){
            for (SysParkingSpaces item : ret){
                if(!item.getId().equals(sysParkingSpacesParm.getId())){
                    throw new Exception("停车位名称已经存在");
                }
            }

        }

        SysParkingSpaces sysParkingSpaces = new SysParkingSpaces();
        BeanUtils.copyProperties(sysParkingSpacesParm, sysParkingSpaces);
        sysParkingSpaces.setBusinessId( bu.getId());


        return toAjax(sysParkingSpacesService.updateSysParkingSpaces(sysParkingSpaces));
    }

    /**
     * 删除【请填写功能名称】
     */
    @ApiOperation(value = "商家删除停车位")
	@DeleteMapping("/delete/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) throws Exception {
        SysBusiness bu = sysBusinessService.getUserInfo();
        if(bu==null){
            throw new Exception("用户未登陆");
        }
        // 商家删除停车位前 判断停车位是否在运营中
        for ( String item : ids){
 /*           Integer status = sysParkingSpacesService.selectSysParkingSpacesStatusById(item);
            if(status==1){
                throw new Exception("停车位占用中，不能删除！");
            }*/

            // 商家关闭停车位前 判断停车位是否在运营中  查询订单
            List<SysOrder> ret = sysOrderService.selectSysOrderByParkingSpacesId(item);
            if(ret.size()>0){
                throw new Exception("停车位占用中，不能删除！");
            }

        }

        return toAjax(sysParkingSpacesService.deleteSysParkingSpacesByIds(ids,bu.getId()));
    }

    /**
     * 删除【请填写功能名称】
     */
    @ApiOperation(value = "商家关闭停车位")
    @PostMapping("/closeParking")
    public AjaxResult closeParking(@RequestBody @Valid  IdParm ids) throws Exception {
        SysBusiness bu = sysBusinessService.getUserInfo();
        if(bu==null){
            throw new Exception("用户未登陆");
        }
        Integer status = sysParkingSpacesService.selectSysParkingSpacesStatusById(ids.getId());
        if(status==2){
            throw new Exception("停车位已经关闭");
        }

        // 商家关闭停车位前 判断停车位是否在运营中  查询订单
        List<SysOrder> ret = sysOrderService.selectSysOrderByParkingSpacesId(ids.getId());
        if(ret.size()>0){
            throw new Exception("停车位占用中，不能关闭！");
        }

        int result = sysParkingSpacesService.updateSysParkingSpacesStatusById(2, bu.getId(), ids.getId());


        return toAjax(result);
    }

    /**
     * 商家停车位可用状态改变成占用状态【请填写功能名称】
     */
    @ApiOperation(value = "商家停车位可用状态改变成占用状态")
    @PostMapping("/availableToOccupation")
    public AjaxResult availableToOccupation(@RequestBody @Valid  IdParm ids) throws Exception {
        SysBusiness bu = sysBusinessService.getUserInfo();
        if(bu==null){
            throw new Exception("用户未登陆");
        }
        Integer status = sysParkingSpacesService.selectSysParkingSpacesStatusById(ids.getId());
        if(status!=0){
            throw new Exception("停车位不是可用状态");
        }
        // 商家关闭停车位前 判断停车位是否在运营中  查询订单
        List<SysOrder> ret = sysOrderService.selectSysOrderByParkingSpacesId(ids.getId());
        if(ret.size()>0){
            throw new Exception("停车位占用中，不能修改！");
        }

        int result = sysParkingSpacesService.updateSysParkingSpacesStatusById(1, bu.getId(), ids.getId());

        return toAjax(result);
    }

    /**
     * 删除【请填写功能名称】
     */
    @ApiOperation(value = "商家停车位占用状态改变成可用状态")
    @PostMapping("/occupationToavailable")
    public AjaxResult occupationToavailable(@RequestBody @Valid  IdParm ids) throws Exception {
        SysBusiness bu = sysBusinessService.getUserInfo();
        if(bu==null){
            throw new Exception("用户未登陆");
        }
        Integer status = sysParkingSpacesService.selectSysParkingSpacesStatusById(ids.getId());
        if(status!=1){
            throw new Exception("停车位不是占用状态");
        }
        // 商家关闭停车位前 判断停车位是否在运营中  查询订单
        List<SysOrder> ret = sysOrderService.selectSysOrderByParkingSpacesId(ids.getId());
        if(ret.size()>0){
            throw new Exception("停车位占用中，不能修改！");
        }

        int result = sysParkingSpacesService.updateSysParkingSpacesStatusById(0, bu.getId(), ids.getId());

        return toAjax(result);
    }
}
